package com.snap.awesomeserial.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.snap.awesomeserial.R;
import com.snap.awesomeserial.adapter.StepEditAdapter;
import com.snap.awesomeserial.base.BaseActivity;
import com.snap.awesomeserial.bean.NAStep;
import com.snap.awesomeserial.bean.TemplateEntity;
import com.snap.awesomeserial.bean.TemplateInfo;
import com.snap.awesomeserial.dialog.NumberInputDialog;
import com.snap.awesomeserial.model.DBManager;
import com.snap.awesomeserial.model.FileManager;
import com.snap.awesomeserial.ui.widget.IconButton;
import com.snap.awesomeserial.utils.FileUtils;
import com.snap.awesomeserial.utils.ToastUtils;

import org.litepal.crud.callback.SaveCallback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class DemoEditActivity extends BaseActivity {

    private static final String TAG = "TemplateActivity";
    @BindView(R.id.title_tv)
    EditText titleTv;
    @BindView(R.id.save_btn)
    IconButton saveBtn;


    @BindView(R.id.steps_rv)
    RecyclerView stepsRv;
    @BindView(R.id.back_btn)
    IconButton backBtn;

    private int currentCheckedId;
    private TemplateInfo templateInfo;
    private StepEditAdapter stepAdapter;
    private TemplateInfo templateInfoOriginal;
    private NumberInputDialog dialog;

    @Override
    protected int layoutResId() {
        return R.layout.activity_demo_edit;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            currentCheckedId = savedInstanceState.getInt("currentCheckId");
        }
        templateInfo = (TemplateInfo) getIntent().getSerializableExtra("templateInfo");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        stepsRv.setLayoutManager(manager);
        stepAdapter = new StepEditAdapter(this);
        stepsRv.setAdapter(stepAdapter);
        stepAdapter.bindDataList(templateInfo == null ? generateDefaultTemp().getNAProgram() : templateInfo.getNAProgram());
//        templateInfoOriginal = (TemplateInfo) templateInfo.clone();
        titleTv.setText(templateInfo.getName());
        titleTv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    titleTv.setBackground(getDrawable(R.drawable.bg_title_focus));
                    titleTv.setSelection(titleTv.getText().length());
                } else {
                    titleTv.setBackground(getDrawable(R.drawable.bg_title));
                }
            }
        });


    }

    private TemplateInfo generateDefaultTemp() {
        templateInfo = new TemplateInfo();
        templateInfo.setName(generateExpNameAutoIncrease());
        List<NAStep> NAProgram = new ArrayList<>();
        templateInfo.setNAProgram(NAProgram);
        return templateInfo;
    }

    private String generateExpNameAutoIncrease() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String fileNamePre = "p" + sdf.format(new Date());
        int i = 1;
        String fileName = fileNamePre + "-" + i;
        while (FileUtils.isExist(FileManager.getInstance().getTempFilePath(fileName))) {
            fileName = fileNamePre + "-" + ++i;
        }
        return fileName;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("currentCheckId", currentCheckedId);
        super.onSaveInstanceState(outState);
    }

    @OnClick({R.id.back_btn, R.id.save_btn, R.id.save2_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.save_btn:
                DBManager.get().saveDemo(new TemplateEntity(templateInfo.getName(),System.currentTimeMillis()), new SaveCallback() {
                    @Override
                    public void onFinish(boolean b) {
                        FileManager.getInstance().saveTempData(templateInfo);
                        DemoEditActivity.this.finish();
                    }
                });
                break;
            case R.id.save2_btn:
                templateInfo.setName(templateInfo.getName() + "COPY1");
                DBManager.get().saveDemo(new TemplateEntity(templateInfo.getName(),System.currentTimeMillis()), new SaveCallback() {
                    @Override
                    public void onFinish(boolean b) {
                        FileManager.getInstance().saveTempData(templateInfo);
                        DemoEditActivity.this.finish();
                        ToastUtils.get().showText("已另存为新项目");
                    }
                });
                break;
            default:
        }
    }
}
